// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace SnapObjects.Expressions
{
    internal class CompileMetadataCollection : MetadataCollection<Expression>
    {
        protected readonly object _lock = new object();
        protected virtual CompileMethodMetaParser MethodMetaParser { get; }
            = new CompileMethodMetaParser();

        public override bool AddGroup(string name, IEnumerable<string> conditions)
        {
            return false;
        }

        public override bool AddGroup(string name, Func<object, object> keySelector)
        {
            return false;
        }

        public override bool AddMethod<TClass>()
        {
            var type = typeof(TClass);

            var methods = this.MethodMetaParser.GetMethodMeta(type);

            foreach (var item in methods)
            {
                if (!_methods.TryGetValue(item.Key, out var method))
                {
                    lock (_lock)
                    {
                        if (!_methods.TryGetValue(item.Key, out method))
                        {
                            _methods[item.Key] = item.Value;

                            continue;
                        }
                    }
                }

                ((CompileMethodMetadata)method).AddMethods(item.Value.OverLoads);
            }

            return true;
        }

        public override bool AddMethod<TClass>(TClass instance)
        {
            return false;
        }

        public override bool AddMethod(string name, Delegate method)
        {
            return false;
        }

        public override bool AddParameter(string name, Type type)
        {
            return false;
        }

        public override bool AddVariable(string name, object value)
        {
            return false;
        }
    }
}
